package com.itheima.project.face.impl;

import com.itheima.project.constant.security.CustomerCacheConstant;
import com.itheima.project.core.enums.security.UserEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.CustomerAdapterFace;
import com.itheima.project.pojo.Resource;
import com.itheima.project.pojo.Role;
import com.itheima.project.pojo.User;
import com.itheima.project.service.ICustomerAdapterService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.security.ResourceVo;
import com.itheima.project.vo.security.RoleVo;
import com.itheima.project.vo.security.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName CustomerAdapterFaceImpl.java
 * @Description 客户权限适配服务接口定义实现
 */
@Slf4j
@Component
public class CustomerAdapterFaceImpl implements CustomerAdapterFace {

    @Autowired
    ICustomerAdapterService customerAdapterService;

    @Override
    @Cacheable(value = CustomerCacheConstant.USERNAME,key = "#username")
    public UserVo findCustomerByUsernameAndEnterpriseId(String username, Long enterpriseId) throws ProjectException {
        try {
            User user = customerAdapterService.findCustomerByUsernameAndEnterpriseId(username,enterpriseId);
            return BeanConv.toBean(user,UserVo.class);
        } catch (Exception e) {
            log.error("查找客户有异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(UserEnum.SELECT_USER_FAIL);
        }
    }

    @Override
    @Cacheable(value = CustomerCacheConstant.MOBILE,key = "#mobil")
    public UserVo findCustomerByMobileAndEnterpriseId(String mobile, Long enterpriseId) throws ProjectException {
        try {
            User user = customerAdapterService.findCustomerByMobileAndEnterpriseId(mobile,enterpriseId);
            return BeanConv.toBean(user,UserVo.class);
        } catch (Exception e) {
            log.error("查找客户有异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(UserEnum.SELECT_USER_FAIL);
        }
    }

    @Override
    @Cacheable(value = CustomerCacheConstant.WECHAT,key = "#openId")
    public UserVo findCustomerByOpenIdAndEnterpriseId(String openId, Long enterpriseId) {
        try {
            User user = customerAdapterService.findCustomerByOpenIdAndEnterpriseId(openId,enterpriseId);
            return BeanConv.toBean(user,UserVo.class);
        } catch (Exception e) {
            log.error("查找客户有异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(UserEnum.SELECT_USER_FAIL);
        }
    }

    @Override
    @Cacheable(value = CustomerCacheConstant.ROLES,key = "#customerId")
    public List<RoleVo> findRoleByCustomerId(Long customerId)throws ProjectException {
        try {
            List<Role> roles = customerAdapterService.findRoleByCustomerId(customerId);
            return BeanConv.toBeanList(roles,RoleVo.class);
        } catch (Exception e) {
            log.error("查找客户有异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(UserEnum.SELECT_ROLE_FAIL);
        }
    }

    @Override
    @Cacheable(value = CustomerCacheConstant.RESOURCES,key = "#customerId")
    public List<ResourceVo> findResourceByCustomerId(Long customerId)throws ProjectException {
        try {
            List<Resource> resources = customerAdapterService.findResourceByCustomerId(customerId);
            return BeanConv.toBeanList(resources,ResourceVo.class);
        } catch (Exception e) {
            log.error("查询客户有资源异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(UserEnum.SELECT_RESOURCE_FAIL);
        }
    }

    @Override
    public UserVo findCurrentCustomer(Long customerId) throws ProjectException {
        //查询用户基本信息
        UserVo userVo = BeanConv.toBean(customerAdapterService.findUserByUserId(customerId),UserVo.class);
        //查询用户资源信息
        List<ResourceVo> resources = this.findResourceByCustomerId(customerId);
        if (!EmptyUtil.isNullOrEmpty(userVo)&&!EmptyUtil.isNullOrEmpty(resources)) {
            Set<String> resourcesLabels = resources.stream().map(ResourceVo::getLabel).collect(Collectors.toSet());
            userVo.setResources(resourcesLabels);
        }
        return userVo;
    }
}
